//导演类，控制游戏的逻辑

import {DataStore} from "./base/DataStore.js";
import { FoodsA } from "./runtime/FoodsA.js";
import { FoodsB } from "./runtime/FoodsB.js";
import { Bottle } from "./runtime/Bottle.js";
import {Gameover} from './runtime/Gameover'
import { Girl } from "./player/Girl.js";


export class Director {

    static getInstance() {
        if (!Director.instance) {
            Director.instance = new Director();
        }
        return Director.instance;
    }

    constructor() {
        this.dataStore = DataStore.getInstance();
        this.moveSpeed = 2;
        this.countNum=0;
        this.bottleCountNum=1;
        this.initStep=1/2 //用来做随机数x轴分区
        this.initCount=0;
        this.birdFlag=false//只注册一个事件
        this.manFlag=false//只注册一个事件
        this.bottomFlag=false//只注册一个事件
        this.state='' //interview是介绍状态
        this.score=0;
    }

    createFoods(){
        const arrx=this.randomNum();
        const arry=this.randomNum();
       const random= Math.floor(Math.random()*10);
      
        if(this.initCount>160){
            this.initCount=0
        }else{
            this.initCount+=40
            arry.forEach((val,index,arr)=>{
                arr[index]+=this.initCount
            })
        }
        if(1<random<3){//随机生成瓶子
           const noBottle= this.dataStore.get('foods').find(val=>{
                return   val.name=='bottle'
                   
                
            })
            if(!noBottle){
                this.dataStore.get('foods').push(new Bottle(arrx[0],arry[0]))
            }
            
        }
        const x=Math.floor(Math.random()*5)*270///雪碧图
        const y=Math.floor(Math.random()*5)*270
        this.dataStore.get('foods').push(new FoodsA(arrx[0],arry[0],5,x,y))
        // this.dataStore.get('foods').push(new FoodsB(arrx[1],arry[1]))
        // this.dataStore.get('foods').push(new FoodsB(arrx[1],arry[2]))
    }



    birdsEvent() {//注册小鸟的事件
        // for (let i = 0; i <= 2; i++) {
        //     this.dataStore.get('birds').y[i] =
        //         this.dataStore.get('birds').birdsY[i];
        // }
        if(!this.birdflag){
            this.dataStore.get('birds').initEvent()
            
            this.birdFlag=true
        }
        
    }
    manEvent(){
        if(!this.manFlag){
            setTimeout(()=>{//避免一进入就选中
                this.dataStore.get('girl').initGirlEvent();//选择女性角色
            },500)
            this.getMan()
            this.manFlag=true
        }
    }
    bottomEvent(){//底部菜单栏
        if(!this.bottomflag){
            this.dataStore.get('bottom').initbottomEvent()
            this.getBottom()
            this.bottomFlag=true
        }
    }

    randomNum(){//不重复的随机数
        const arr=[];
        for(let p=0;p<3;p++){
            const num=Math.floor(Math.random()*DataStore.getInstance().canvas.width)
            if(!arr.includes(num)){
                if(this.initStep==1){
                    this.initStep=1/2
                }else{
                    this.initStep+=1/2
                }
                // console.log(this.initStep)
                arr.push(num*this.initStep)
            }
        }
        return arr
    }

    check(){//判断小鸟位置与下落物理是否接触了
        const foods=this.dataStore.get('foods')
        const birds=this.dataStore.get('birds')
        // console.log(birds.birdsY)
        const birdsY=birds.birdsY;//小鸟的y坐标
        const birdsX=birds.birdsX;//小鸟的y坐标
        foods.forEach((val,index,arr)=>{
            if(val.x<=birdsX+40 && birdsX<=val.x+40 && birdsY>=val.y && birdsY<=val.y+40){
                arr.splice(index,1)
                this.dataStore.get('score').scoreNumber+=val.score
                this.score+=val.score
                if(val.name=='bottle'){
                    this.dataStore.get('score').bottle+=val.score
                }
            }
        })
    }

    getMan(){
        const getMan1=   setInterval(()=>{
            if(!this.bottom){
                if(!this.dataStore.get('girl').hasInitEvent){
                    this.dataStore.get('girl').initEvent()
                }
                if(!this.man ){
                    this.man=this.dataStore.get('girl').returnGirl()
                    console.log('woxuanrenle')
                }else{
                     console.log('xianshitanchuang')
                    clearInterval(getMan1)
                    this.expampleToPlay()
 
                    
                }
            }else{
                this.dataStore.get('girl').removeGirlEvent()
            }
               

               console.log(this.man)
           },500)

       }

       getBottom(){
        const getBottom1=   setInterval(()=>{
              this.bottom=this.dataStore.get('bottom').returnBottom()
              switch(this.bottom){
                default:
                if(!this.man){
                    this.interview()
                }
                  
                  break;

                  case 'bottom1':
                  this._interviewBase()
                  this.dataStore.get('rule').drawRule();break;
                  case 'bottom2':
                  this._interviewBase()
                  this.dataStore.get('rule').drawMe();break;
                  case 'bottom4':
                  this._interviewBase()
                  this.dataStore.get('rule').drawRanking();
                  this.dataStore.get('rule').drawRankingList();
                  this.dataStore.get('rule').initArrowDEvent();//注册事件
                  break;
              }

              if(this.man){
                  clearInterval(getBottom1)
              }
           },500)

       }

    expampleToPlay(){//游戏示范
        this.dataStore.get('example').draw();
        // this.dataStore.get('interview').drawInterviewTit();
        
        this.dataStore.get('bottom').drawbottom1()
        this.dataStore.get('bottom').drawbottom2()
        this.dataStore.get('bottom').drawbottom3()
        this.dataStore.get('bottom').drawbottom4();

        Director.showExample=true;//发布给main
        Director.man=this.man
    }
    _interviewBase(){//绘制介绍页基础类
        this.dataStore.get('background').draw();
        this.dataStore.get('interview').drawInterviewTit();
        this.dataStore.get('bottom').drawbottom1()
        this.dataStore.get('bottom').drawbottom2()
        this.dataStore.get('bottom').drawbottom3()
        this.dataStore.get('bottom').drawbottom4()
    }
    interview(){ //绘制介绍页
        this._interviewBase()
        this.dataStore.get('interview').drawInterviewBorder();

        this.dataStore.get('girl').drawgirl1();
        this.dataStore.get('girl').drawgirl2();
        this.dataStore.get('girl').drawgirl3();
        this.dataStore.get('girl').drawgirl4();

        
    }
    showGameover(){
        this.dataStore.put('gameover',Gameover)
        this.dataStore.get('gameover').draw()
        this.dataStore.get('gameover').drawEndTit()
        this.dataStore.get('gameover').drawEndHand()
    }
    run() {
        // if(this.state=='interview'){
        //     this.dataStore.get('background').draw();
        // }
        if(this.score=='5'){
            this.isGameOver=true
        }
        if (!this.isGameOver) {
            
            this.dataStore.get('background').draw();
            this.dataStore.get('birds').draw();
            this.dataStore.get('score').drawInterviewTit()
            this.dataStore.get('score').draw()
            this.dataStore.get('score').drawGold()

            const foods=this.dataStore.get('foods')
            this.countNum+=0.01
            if(this.countNum>1){
                this.createFoods()
                this.countNum=0;
                if(this.dataStore.get('birds').jumpHeightTotal!=0){
                    this.dataStore.get('birds').draw()
                }
            }
            this.check()

            if(foods.length>=20){//每20个删除一项，瓶子也是
                foods.shift()
            }

            

            this.dataStore.get('foods').forEach(val=>{
                val.draw()
            })
            
            let timer = requestAnimationFrame(() => this.run());
            this.dataStore.put('timer', timer);
        } else {
            console.log('游戏结束');
            // this.dataStore.get('startButton').draw();
            cancelAnimationFrame(this.dataStore.get('timer'));
            this.dataStore.destroy();
            //触发微信小游戏垃圾回收
            this.showGameover()
            wx.triggerGC();
        }
    }
}